package cn.itcast.service.impl;


import cn.itcast.dao.MemberDao;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.Result;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.Member;
import cn.itcast.service.MemberService;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.Date;

@Service
public class MemberServiceImpl  implements MemberService {

    @Autowired
    private MemberDao memberDao;



    Result result =new Result();
    String regularExpression = "(^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|" +
            "(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}$)";



    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        /*分页查询，查询默认在第一页查询*/
    /*if ( queryString != null){
        currentPage = 1;
    }*/
        PageHelper.startPage(currentPage, pageSize);
        Page<Member> page = memberDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }


    @Override
    public Result addMemBer(Member member) {

        member.setStatus(0);
        Date date =new Date();
        member.setRegTime(date);

        if(member.getName()==null){
            result.setFlag(false);
            result.setMessage("姓名不能为空！");
            return result;
        }
        if ((member.getIdCard() != null)){
            if (member.getIdCard().length() != 18 && (!member.getIdCard().matches(regularExpression))){
                result.setFlag(false);
                result.setMessage("身份证必须是18位！");
                return result;
            }
        }
        if (member.getPhoneNumber().length() != 11){
            result.setFlag(false);
            result.setMessage("电话号码格式不对！");
            return result;
        }
        if ((member.getEmail() != null)) {
            if (!(member.getEmail().endsWith(".com") && member.getEmail().contains("@"))) {
                result.setFlag(false);
                result.setMessage("邮箱格式不正确！");
                return result;
            }
        }
        result.setFlag(true);
        result.setMessage("新增成功！");
        //保存会员信息
        memberDao.addMember(member);
        return result;
    }

    @Override
    public void update(Member member) {
        if(member.getName()==null){
            result.setFlag(false);
            result.setMessage("姓名不能为空！");

        }
        if ((member.getIdCard() != null)){
            if (member.getIdCard().length() != 18 && (!member.getIdCard().matches(regularExpression))){
                result.setFlag(false);
                result.setMessage("身份证必须是18位！");

            }
        }
        if (member.getPhoneNumber().length() != 11){
            result.setFlag(false);
            result.setMessage("电话号码格式不对！");

        }
        result.setFlag(true);
        result.setMessage("编辑成功！");
        memberDao.update(member);

    }


    @Override
    public void add(Member member) {
        this.memberDao.add(member);
    }


    @Override
    public Member findById(Integer id) {
        return memberDao.findById(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        Member member =new Member();
        member.setStatus(status);
        member.setId(id);
        memberDao.update(member);

    }

    @Override
    public Member findByTelephone(String phone) {
        return memberDao.findByTelephone(phone);
    }
}
